package com.briup.test;

/**
 * @author 甜圆
 * @version v1.0.1
 * @date 2025/8/25 19:34
 * @package com.briup.test
 * @copyright 甜圆
 */
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        // 记录字符上一次出现的位置
        int[] last = new int[128];
        for(int i = 0; i < 128; i++) {
            last[i] = -1;
        }
        int n = s.length();

        int res = 0;
        int start = 0; // 窗口开始位置
        for(int i = 0; i < n; i++) {
            int index = s.charAt(i);
            start = Math.max(start, last[index] + 1);
            res   = Math.max(res, i - start + 1);
            last[index] = i;
        }

        return res;
    }

    // 主方法，程序入口
    public static void main(String[] args) {
        Solution solution = new Solution();

        // 测试用例
        String test1 = "abcabcbb";
        String test2 = "bbbbb";
        String test3 = "pwwkew";
        String test4 = "";

        // 输出测试结果
        System.out.println("测试用例1: " + test1 + "，结果: " + solution.lengthOfLongestSubstring(test1));
        System.out.println("测试用例2: " + test2 + "，结果: " + solution.lengthOfLongestSubstring(test2));
        System.out.println("测试用例3: " + test3 + "，结果: " + solution.lengthOfLongestSubstring(test3));
        System.out.println("测试用例4: " + test4 + "，结果: " + solution.lengthOfLongestSubstring(test4));
    }
}
